﻿using FileTransferDesktop.Models;
using FileTransferDesktop.Services;
using Wpf.Ui.Abstractions.Controls;
using Wpf.Ui.Appearance;

namespace FileTransferDesktop.ViewModels.Pages;

public partial class SettingsViewModel : ObservableObject, INavigationAware
{
    private readonly IAppSettingsService _settingsService;
    private bool _isInitialized = false;
    private AppSettings _currentSettings;

    public SettingsViewModel(IAppSettingsService settingsService)
    {
        _settingsService = settingsService;
        _currentSettings = _settingsService.GetSettings();

        // 监听设置变化
        _settingsService.SettingsChanged += OnSettingsChanged;
    }

    private void OnSettingsChanged(object? sender, AppSettings settings)
    {
        _currentSettings = settings;
        // 更新UI绑定的属性
        OnPropertyChanged(nameof(DownloadPath));
    }

    [ObservableProperty]
    private ApplicationTheme _currentTheme = ApplicationTheme.Unknown;

    public Task OnNavigatedToAsync()
    {
        if (!_isInitialized)
            InitializeViewModel();
        else
        {
            // 即使已经初始化，也要确保主题状态是最新的
            _currentSettings = _settingsService.GetSettings();
            ApplyThemeFromSettings();
        }

        return Task.CompletedTask;
    }

    public Task OnNavigatedFromAsync() => Task.CompletedTask;

    private void InitializeViewModel()
    {
        // 从设置服务加载设置
        _currentSettings = _settingsService.GetSettings();

        // 根据保存的主题设置应用主题
        ApplyThemeFromSettings();

        OnPropertyChanged(nameof(DownloadPath));

        _isInitialized = true;
    }

    private void ApplyThemeFromSettings()
    {
        if (_currentSettings != null)
        {
            switch (_currentSettings.Theme)
            {
                case "Light":
                    ApplicationThemeManager.Apply(ApplicationTheme.Light);
                    CurrentTheme = ApplicationTheme.Light;
                    break;
                case "Dark":
                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    CurrentTheme = ApplicationTheme.Dark;
                    break;
                default:
                    // 默认使用系统主题或当前主题
                    CurrentTheme = ApplicationThemeManager.GetAppTheme();
                    break;
            }
        }
        else
        {
            CurrentTheme = ApplicationThemeManager.GetAppTheme();
        }
    }

    [RelayCommand]
    private void OnChangeTheme(string parameter)
    {
        switch (parameter)
        {
            case "theme_light":
                if (CurrentTheme == ApplicationTheme.Light)
                    break;

                ApplicationThemeManager.Apply(ApplicationTheme.Light);
                CurrentTheme = ApplicationTheme.Light;
                SaveThemeSetting("Light");

                break;

            default:
                if (CurrentTheme == ApplicationTheme.Dark)
                    break;

                ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                CurrentTheme = ApplicationTheme.Dark;
                SaveThemeSetting("Dark");

                break;
        }
    }

    private void SaveThemeSetting(string theme)
    {
        if (_currentSettings != null)
        {
            _currentSettings.Theme = theme;
            _settingsService.SaveSettings(_currentSettings);
        }
    }

    public string DownloadPath
    {
        get =>
            _currentSettings?.DownloadPath
            ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
        set
        {
            if (_currentSettings != null && _currentSettings.DownloadPath != value)
            {
                _currentSettings.DownloadPath = value;
                _settingsService.SaveSettings(_currentSettings);
                OnPropertyChanged();
            }
        }
    }

    [RelayCommand]
    private void ChangeDownloadPath()
    {
        var dialog = new System.Windows.Forms.FolderBrowserDialog
        {
            Description = "请选择新的下载文件夹",
            SelectedPath = DownloadPath,
        };
        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            DownloadPath = dialog.SelectedPath; // 这会自动保存到设置服务
        }
    }

    public string ThemeDisplay =>
        CurrentTheme switch
        {
            ApplicationTheme.Light => "浅色",
            ApplicationTheme.Dark => "深色",
            _ => "未知",
        };
    public bool IsDarkMode
    {
        get => CurrentTheme == ApplicationTheme.Dark;
        set
        {
            if (value)
            {
                if (CurrentTheme != ApplicationTheme.Dark)
                {
                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    CurrentTheme = ApplicationTheme.Dark;
                    SaveThemeSetting("Dark");
                }
            }
            else
            {
                if (CurrentTheme != ApplicationTheme.Light)
                {
                    ApplicationThemeManager.Apply(ApplicationTheme.Light);
                    CurrentTheme = ApplicationTheme.Light;
                    SaveThemeSetting("Light");
                }
            }
        }
    }

    partial void OnCurrentThemeChanged(ApplicationTheme value)
    {
        OnPropertyChanged(nameof(ThemeDisplay));
    }
}
